﻿using System;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Input;
using FitnessHelperDB;


namespace FitnessHelperDesktopClient
{
  public partial class MainWindow
  {
    #region Свойства

    public FitnessHelperModelContainer DbContext { get; set; }

    public DateTime SelectedDate { get; private set; }

    public ObservableCollection<CalendarRecords> SelectedDateRecords { get; private set; }
    #endregion


    #region Конструктор
    public MainWindow()
    {
      InitializeComponent();

      DbContext = new FitnessHelperModelContainer();
      DataContext = this;

      SelectedDate = DateTime.Now;

      SelectedDateRecords = new ObservableCollection<CalendarRecords>();
      var curHour = 8;
      while (curHour < 24)
      {
        SelectedDateRecords.Add(new CalendarRecords
        {
          Date = new DateTime(SelectedDate.Year, SelectedDate.Month, SelectedDate.Day, curHour, 0, 0)
        });
        SelectedDateRecords.Add(new CalendarRecords
        {
          Date = new DateTime(SelectedDate.Year, SelectedDate.Month, SelectedDate.Day, curHour, 30, 0)
        });
        ++curHour;
      }

      var dateRecords = DbContext.CalendarRecords.Where(record => record.Date.Year == SelectedDate.Year &&
                                                                  record.Date.Month == SelectedDate.Month &&
                                                                  record.Date.Day == SelectedDate.Day);
      foreach (var dateRecord in dateRecords)
      {
        SelectedDateRecords.Add(dateRecord);
      }
    }
    #endregion


    #region Обработка событий
    private void Window_Closed(object sender, EventArgs e)
    {
      DbContext.Dispose();
    }


    #region Меню
    private void MenuFileExit_Click(object sender, RoutedEventArgs e)
    {
      Application.Current.Shutdown();
    }


    private void MenuUtilsFoods_Click(object sender, RoutedEventArgs e)
    {
      var foodsWindow = new FoodsWindow();

      foodsWindow.ShowDialog();
    }


    private void MenuUtilsFoodPlans_Click(object sender, RoutedEventArgs e)
    {
      var foodPlansWindow = new FoodPlansWindow();

      foodPlansWindow.ShowDialog();
    }


    private void MenuUtilsRecords_Click(object sender, RoutedEventArgs e)
    {
      var weightRecordsWindow = new WeightRecordsWindow();

      weightRecordsWindow.ShowDialog();
    }


    private void MenuUtilsProgramms_Click(object sender, RoutedEventArgs e)
    {
      var programmsWindow = new ProgrammsWindow();

      programmsWindow.ShowDialog();
    }
    #endregion


    private void ProgrammDaysDataGrid_MouseLeave(object sender, MouseEventArgs e)
    {
      if (e.LeftButton == MouseButtonState.Pressed &&
          ProrammDaysDataGrid.CurrentCell.IsValid)
      {
        var programmDay = (ProgrammDays)ProrammDaysDataGrid.CurrentCell.Item;
        var dataObject = new DataObject();

        dataObject.SetData(DataFormats.Text, programmDay.Name, false);
        DragDrop.DoDragDrop(ProrammDaysDataGrid, dataObject, DragDropEffects.Copy);
      }
    }
    #endregion
  }
}
